package com.dz.control;

import com.dz.bean.count;
import com.dz.bean.user;
import com.dz.dao.userMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class Users {

    @RequestMapping("/add")
    public String useradd(){

        return "index";
    }
    @Autowired
    private userMapper userMapper;


    @GetMapping("countuser")
    @ResponseBody
    public Integer count(){
        Integer i=  userMapper.countuser();
      return i;

    }
    @GetMapping("countuserdizhi")
    @ResponseBody
    public List<Map> count1(){
        List<Map>  i=  userMapper.countuserdizhi();
        return i;

    }


    @RequestMapping("/insert")
    public String userInsert(HttpServletRequest request){
       String name= request.getParameter("name");
       String username=request.getParameter("username");
       String password=request.getParameter("password");
       String dizhi=request.getParameter("dizhi");
       String lianxi=request.getParameter("phone");
       String sex=request.getParameter("sex");
        user us =new user();
        us.setPassword(password);
        us.setUsername(username);
        us.setName(name);
        us.setDizhi(dizhi);
        us.setLianxi(lianxi);
        us.setSex(sex);
        System.out.println(us);
        int userinsert = userMapper.userinsert(us);
        System.out.println(userinsert);



        return "member-list.html";

    }
    @GetMapping(value = "/deleteUser/{id}")
    @ResponseBody
    public String deleteUser(@PathVariable("id") Integer id){
       int i= userMapper.deleteUser(id);
       if (i>0){
           return "删除成功"+id;
       }else {
           return "删除失败";
       }

    }

    @GetMapping(value = "/updateUser")
    @ResponseBody
    public String updateUser(user user){

        user us=new user();
        us.setId(6);
        us.setName("张三");
        us.setUsername("user99");
        us.setPassword("123");

        int i= userMapper.updateByPrimaryKey(us);
        if (i>0){
            return "更新成功";
        }else {
            return "更新失败";
        }

    }


}
